//
//  ProductListViewController.m
//  NewApp
//
//  Created by  on 12-6-15.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ProductListViewController.h"
#import "ProductList_A_ViewController.h"
#import "ProductList_BCD_ViewController.h"
#import "ProductData.h"

@interface ProductListViewController ()

@end

@implementation ProductListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self.engine productInfosWithPage:1];
	// Do any additional setup after loading the view.
}

- (void)viewDidUnload
{
    if (viewCtrlA != NULL) {
        [viewCtrlA release];
    }
    
    if (viewCtrlB) {
        [viewCtrlB release];
    }
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

#pragma mark -
#pragma mark - Private methods

- (void) requestDidFinished:(RequestEngine *)reqEngine withObject:(id)object {
    if ([object isKindOfClass:[NSDictionary class]]) {
        NSDictionary *result = (NSDictionary *) object;
        [result retain];
        
       // self.titleLabel.text = [result objectForKey:@"top_title"];
        NSString *style = [result objectForKey:@"style"];
        NSArray *thumbnails = [[result objectForKey:@"data"] retain];
        NSMutableArray *thumbnailImages = [[NSMutableArray alloc] init];

        if ([style isEqualToString:@"A"]) {
            for (NSDictionary *dic in thumbnails) {
                NAImage *product = [[NAImage alloc] initWithDictionary:dic];
                [thumbnailImages addObject:product];
            }

            viewCtrlA = [[ProductList_A_ViewController alloc] init];
            viewCtrlA.titleText = [result objectForKey:@"top_title"];
            viewCtrlA.dataArray = [[NSMutableArray alloc] initWithArray:thumbnailImages];
            [viewCtrlA.view setFrame:CGRectMake(0, 26, 320, 480)];
//            [self.view addSubview:viewCtrlA.view];
            [self.navigationController pushViewController:viewCtrlA animated:NO];
            //[viewCtrl release];
        }else {
            for (NSDictionary *dic in thumbnails) {
                ProductEntity *product = [[ProductEntity alloc] initWithDictionary:dic];
                [thumbnailImages addObject:product];
            }
            viewCtrlB = [[ProductList_BCD_ViewController alloc] init];
            viewCtrlB.bHideBack = NO;
            viewCtrlB.titleText = [result objectForKey:@"top_title"];
            viewCtrlB.dataArray = [[NSMutableArray alloc] initWithArray:thumbnailImages];
            viewCtrlB.listStyle = [style retain];
            self.title = @"返回";
            [viewCtrlB.view setFrame:CGRectMake(0, 26, 320, 480)];
            //[self.navigationController pushViewController:viewCtrlB animated:NO];
            self.view = viewCtrlB.view;
            [viewCtrlB reloadInputViews];

        }

        [thumbnailImages release];
        [thumbnails release];
        [result release];
    }
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
